package action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import model.Baokao;
import model.Kemu;
import model.User;

import org.apache.struts2.ServletActionContext;

import util.Util;


import com.opensymphony.xwork2.ActionSupport;

import dao.BaokaoDao;
import dao.KemuDao;
import dao.UserDao;

public class ManageAction extends ActionSupport {

	private static final long serialVersionUID = -4304509122548259589L;

	private File filedata;
	private String filedataFileName;


	public File getFiledata() {
		return filedata;
	}


	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}


	public String getFiledataFileName() {
		return filedataFileName;
	}


	public void setFiledataFileName(String filedataFileName) {
		this.filedataFileName = filedataFileName;
	}
	
	private UserDao userDao;

	private String url = "./";

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	//程序登入界面
	public String index() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Util.init(request);
		if (request.getSession().getAttribute("user") == null) {
			return "success2";

		} else {
			return "success1";
		}

	}
//登入请求
	public String login() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String  role = request.getParameter("role");
		User user = userDao.selectBean(" where username = '" + username
				+ "' and password= '" + password + "' and role= "+role+" and userlock=0");
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			this.setUrl("index");
			return "redirect";
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('用户名或者密码错误');window.location.href='index';</script>");
		}
		return null;
	}
//用户退出
	public String loginout() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		this.setUrl("login.jsp");
		return SUCCESS;
	}
//跳转到修改密码页面
	public String changepwd() {
		this.setUrl("user/user.jsp");
		return SUCCESS;
	}
//修改密码操作
	public void changepwd2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		String password3 = request.getParameter("password3");
		User bean = userDao.selectBean(" where username= '" + u.getUsername()
				+ "' and password= '" + password1 + "'");
		if (!password2.equals(password3)) {
			out.print(Util.tiaozhuan2("两次输入密码不一致", "index", ""));
			out.flush();
			out.close();
		} else if (bean != null) {
			bean.setPassword(password2);
			userDao.updateBean(bean);
			out.print(Util.tiaozhuan2("操作成功", "index", ""));
			out.flush();
			out.close();
		} else {
			out.print(Util.tiaozhuan2("原密码错误", "index", ""));
			out.flush();
			out.close();
		}
	}
//跳转到注册页面
	public String register() {
		this.setUrl("register.jsp");
		return SUCCESS;
	}
//用户注册操作
	public void register2() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String truename = request.getParameter("truename");
		String mobile = request.getParameter("mobile");


		User bean = userDao.selectBean(" where username='" + username
				+ "' and userlock=0");
		if (bean == null) {
			bean = new User();
			bean.setCreatetime(new Date());
			bean.setPassword(password);
			bean.setRole(0);
			bean.setUsername(username);
			bean.setTruename(truename);
			bean.setMobile(mobile);
			userDao.insertBean(bean);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('注册成功,请登录系统完善个人信息，通过管理员审核才能报考');window.location.href='index';</script>");
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
					.getWriter()
					.print(
							"<script language=javascript>alert('该用户已经注册，请不要重复注册');window.location.href='method!register';</script>");
		}

	}
	
	
	//跳转到修改个人照片页面
	public String userupdate0() {
		HttpServletRequest request = ServletActionContext.getRequest();
		User bean = userDao.selectBean(" where id= "
				+ request.getParameter("id"));
		request.setAttribute("bean", bean);
		
		this.setUrl("user/userupdate0.jsp");
		return SUCCESS;
	}

	
	//跳转到添加添加照片页面
	public String upload(){
		this.setUrl("user/upload.jsp");
		return SUCCESS;
	}
	
	//添加照片操作
	 public String upload2() throws Exception{
			String path = ServletActionContext.getServletContext().getRealPath(
					"/uploadfiles/");
			File f1 = new File(path);
			if (!f1.exists()) {
				f1.mkdirs();
			}
		
			String name = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS")
					.format(new Date()).toString();
			String filename = name
					+ filedataFileName.substring(filedataFileName.lastIndexOf("."));
			File file = new File(path, filename);
			util.Util.copyFile(filedata, file);
			
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setContentType("text/html;charset=UTF-8");
			resp.setCharacterEncoding("utf-8");
			
			PrintWriter out = resp.getWriter();
			out.print("{\"id\":\"\",\"fileName\":\"" + filedataFileName
					+ "\",\"attachmentPath\":\"" + filename
					+ "\",\"attachmentSize\":\"\"}");
			return null;
	}
	 
	//添加用户照片操作
		public String userupdate02() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			
			String photo1=request.getParameter("dwz.attachment.attachmentPath");
			
			User user = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			if(user.getStatus()==1){
				request.setAttribute("info", "该用户个人信息已经审核通过，上传失败！！");
			}else{
				user.setPhoto(photo1);
				userDao.updateBean(user);
				request.setAttribute("info", "上传成功，请点击个人照片管理获取个人照片");
			}
			this.setUrl("user/userupdate0k.jsp");
			return SUCCESS;
	}
		
		
		//用户个人管理列表
		public String userlist2() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String id = request.getParameter("id");
			
			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");
			if (id != null && !"".equals(id)) {
				sb.append("id =" + id + "");
				sb.append(" and ");
				sb2.append("id =" + id + "");
				sb2.append(" and ");
				request.setAttribute("id", id);

			}

			
			sb.append(" userlock=0  order by id desc ");
			String where = sb.toString();
			sb2.append(" userlock=0   ");
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = userDao.selectBeanCount(where2);
			request.setAttribute("list", userDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			this.setUrl("user/userlist2.jsp");
			return SUCCESS;
		}
	
		
		
		//跳转到修改用户个人信息
		public String userupdate() {
			HttpServletRequest request = ServletActionContext.getRequest();
			User bean = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			request.setAttribute("bean", bean);
			String userto=request.getParameter("userto");
			request.setAttribute("userto", userto);
			this.setUrl("user/userupdate.jsp");
			return SUCCESS;
		}
		
		//用户个人信息修改操作
		public void userupdate2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();

			String sfz = request.getParameter("sfz");
			String address = request.getParameter("address");
			String truename = request.getParameter("truename");
			String birthday = request.getParameter("birthday");
			String sex = request.getParameter("sex");
			String jiguan = request.getParameter("jiguan");
			String minzu=request.getParameter("minzu");
			String mobile=request.getParameter("mobile");
			 String whcd=request.getParameter("whcd");
			 String zhiye=request.getParameter("zhiye");
			
			
		
			User user = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			user.setAddress(address);
			user.setBirthday(birthday);
			user.setCreatetime(new Date());
			user.setJiguan(jiguan);
			user.setMinzu(minzu);
			user.setMobile(mobile);
			user.setSex(sex);
			user.setSfz(sfz);
			user.setTruename(truename);
			user.setWhcd(whcd);
			user.setZhiye(zhiye);
			userDao.updateBean(user);
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			String userto=request.getParameter("userto");
			if(!"1".equals(userto)){
				out.print(Util.tiaozhuan("操作成功", "method!userlist", "userlist"));
			}else{
				out.print(Util.tiaozhuan("操作成功", "method!userlist2?id="+user.getId(), "userlist2"));
			}
			
			out.flush();
			out.close();
		}
		
		
		//用户管理列表
		public String userlist() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String truename = request.getParameter("truename");
			String status = request.getParameter("status");
			String id = request.getParameter("id");
			
			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");
			if (id != null && !"".equals(id)) {
				sb.append("id =" + id + "");
				sb.append(" and ");
				sb2.append("id =" + id + "");
				sb2.append(" and ");
				request.setAttribute("id", id);

			}

			if (truename != null && !"".equals(truename)) {
				sb.append("truename like '%" + truename + "%'");
				sb.append(" and ");
				sb2.append("truename like '%" + truename + "%'");
				sb2.append(" and ");
				request.setAttribute("truename", truename);

			}
			if (status != null && !"".equals(status)) {
				sb.append("status = " + status + " and " );
				request.setAttribute("status", status);

			}


			sb.append(" userlock=0 and role=0 order by id desc ");
			String where = sb.toString();
			sb2.append(" userlock=0 and role=0  ");
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = userDao.selectBeanCount(where2);
			request.setAttribute("list", userDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			request.setAttribute("url", "method!userlist");
			
			this.setUrl("user/userlist.jsp");
			return SUCCESS;
		}
		
		
		//用户删除操作
		public void userdelete() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			User bean = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			bean.setUserlock(1);
			userDao.updateBean(bean);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(Util.tiaozhuan2("操作成功", "method!userlist", "userlist"));
			out.flush();
			out.close();
		}
		
		
		//跳转到审核个人信息页面
		public String userupdate9() {
			HttpServletRequest request = ServletActionContext.getRequest();
			User bean = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			request.setAttribute("bean", bean);
			this.setUrl("user/userupdate9.jsp");
			return SUCCESS;
		}
		
		
		//审核个人信息操作
		public void userupdate92() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String status = request.getParameter("status");
		
			User bean = userDao.selectBean(" where id= "
					+ request.getParameter("id"));
			bean.setStatus(Integer.parseInt(status));
			
			userDao.updateBean(bean);
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			out.print(Util.tiaozhuan("操作成功", "method!userlist", "userlist"));
			out.flush();
			out.close();
		}
		
		
		//批量审核通过操作
		public void shenhe1() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String[] ids = request.getParameterValues("ids");
			for(int i=0;i<ids.length;i++){
				User bean = userDao.selectBean(" where id= " + ids[i]);
				
				bean.setStatus(1);
				userDao.updateBean(bean);
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('操作成功');window.location.href='index';</script>");
		}
		
		//批量审核不通过操作
		public void shenhe2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String[] ids = request.getParameterValues("ids");
			for(int i=0;i<ids.length;i++){
				User bean = userDao.selectBean(" where id= " + ids[i]);
				if(bean.getStatus()==0){
					bean.setStatus(-1);
				
				}
				userDao.updateBean(bean);
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
			response
			.getWriter()
			.print(
					"<script language=javascript>alert('操作成功');window.location.href='index';</script>");
		}
		
		public void userlistdownload() throws RowsExceededException, WriteException, IOException{

	    	HttpServletResponse response = ServletActionContext.getResponse();
	    	HttpServletRequest req = ServletActionContext.getRequest();
	    	response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=\""
					+ new String("导出考生数据列表.xls"
							.getBytes("gb18030"),
							"iso8859-1") + "\"");

			
			//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象  
			WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());
			//创建一个可写入的工作表    
	        //Workbook的createSheet方法有两个参数，第一个是工作表的名称，第二个是工作表在工作薄中的位置    
	        WritableSheet ws = wwb.createSheet("sheet1", 0); 
	        
			StringBuffer where=new StringBuffer();

				where.append(" where status=1 and userlock=0 and role=0");

				if(req.getParameter("id")!=null&&req.getParameter("id").length()>0)
					{
					where.append(" and id="+req.getParameter("id"));
					
					}
			List<User> list = userDao.selectBeanList(0, 20, where.toString());
	    	ws.addCell(new Label(0,0,"导出考生数据列表"));
			Label lab = new Label(0, 1,"姓名");
			ws.addCell(lab);
			lab=new Label(1, 1,"报考项目");
			ws.addCell(lab);
			lab=new Label(2, 1,"联系电话");
			ws.addCell(lab);
			lab=new Label(3, 1,"备注");
			ws.addCell(lab);
			lab=new Label(4, 1,"出生年月");
			ws.addCell(lab);
			lab=new Label(5, 1,"联系地址");
			ws.addCell(lab);
			lab=new Label(6, 1,"注册时间");
			ws.addCell(lab);
			
			
			for(int i=0;i<list.size();i++){		
				Label lab1 = new Label(0, i+2,list.get(i).getTruename());
				ws.addCell(lab1);
				Label lab2 = new Label(1, i+2,list.get(i).getBkxm());
				ws.addCell(lab2);
				Label lab3 = new Label(2, i+2,list.get(i).getMobile());
				ws.addCell(lab3);
				Label lab4 = new Label(3, i+2,list.get(i).getZhiye());
				ws.addCell(lab4);
				Label lab5 = new Label(4, i+2,list.get(i).getBirthday());
				ws.addCell(lab5);
				Label lab6 = new Label(5, i+2,list.get(i).getAddress());
				ws.addCell(lab6);
				Label lab7 = new Label(6, i+2,list.get(i).getCreatetime().toString());
				ws.addCell(lab7);
			}
			//从内存中写入文件中    
	        wwb.write();
	        //关闭资源，释放内存    
	        wwb.close(); 
			
		}
		
		
		private KemuDao kemuDao;


		public KemuDao getKemuDao() {
			return kemuDao;
		}


		public void setKemuDao(KemuDao kemuDao) {
			this.kemuDao = kemuDao;
		}

		
		//科目列表管理
		public String kemulist() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");

			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");

			if (name != null && !"".equals(name)) {
				sb.append("name like '%" + name + "%'");
				sb.append(" and ");
				sb2.append("name like '%" + name + "%'");
				sb2.append(" and ");
				request.setAttribute("name", name);

			}

			sb.append(" kemulock=0  order by id desc ");
			String where = sb.toString();
			sb2.append(" kemulock=0  ");
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = kemuDao.selectBeanCount(where2);
			request.setAttribute("list", kemuDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			request.setAttribute("url", "method!kemulist");
			this.setUrl("kemu/kemulist.jsp");
			return SUCCESS;
		}
	//跳转到增加科目页面
		public String kemuadd() {
			this.setUrl("kemu/kemuadd.jsp");
			return SUCCESS;
		}
	//增加科目操作
		public void kemuadd2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");
			String jibie = request.getParameter("jibie");
			String jieshu = request.getParameter("jieshu");
			String kaishi = request.getParameter("kaishi");
			String kaoshishijian = request.getParameter("kaoshishijian");
			
			Kemu bean = new Kemu();
			bean.setName(name);
			bean.setJibie(jibie);
			bean.setJieshu(jieshu);
			bean.setKaishi(kaishi);
			bean.setKaoshishijian(kaoshishijian);
			kemuDao.insertBean(bean);
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			out.print(Util.tiaozhuan("操作成功", "method!kemulist", "kemulist"));
			out.flush();
			out.close();

		}
	//跳转到更新科目页面
		public String kemuupdate() {
			HttpServletRequest request = ServletActionContext.getRequest();
			Kemu bean = kemuDao.selectBean(" where id= "
					+ request.getParameter("id"));
			request.setAttribute("bean", bean);
			this.setUrl("kemu/kemuupdate.jsp");
			return SUCCESS;
		}
		
		
	//更新科目操作
		public void kemuupdate2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");
			String jibie = request.getParameter("jibie");
			String jieshu = request.getParameter("jieshu");
			String kaishi = request.getParameter("kaishi");
			String kaoshishijian = request.getParameter("kaoshishijian");

			Kemu bean = kemuDao.selectBean(" where id= "
					+ request.getParameter("id"));

			bean.setName(name);
			bean.setJibie(jibie);
			bean.setJieshu(jieshu);
			bean.setKaishi(kaishi);
			bean.setKaoshishijian(kaoshishijian);
			kemuDao.updateBean(bean);
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			out.print(Util.tiaozhuan("操作成功", "method!kemulist", "kemulist"));
			out.flush();
			out.close();
		}
	//删除科目操作
		public void kemudelete() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			Kemu bean = kemuDao.selectBean(" where id= "
					+ request.getParameter("id"));
			
			bean.setKemulock(1);
			
			kemuDao.updateBean(bean);
			

			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(Util.tiaozhuan2("操作成功", "method!kemulist", "kemulist"));
			out.flush();
			out.close();
		}
		
		private BaokaoDao baokaoDao;


		public BaokaoDao getBaokaoDao() {
			return baokaoDao;
		}


		public void setBaokaoDao(BaokaoDao baokaoDao) {
			this.baokaoDao = baokaoDao;
		}
		
		
		//科目列表管理
		public String kemulist2() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");

			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");

			if (name != null && !"".equals(name)) {
				sb.append("name like '%" + name + "%'");
				sb.append(" and ");
				sb2.append("name like '%" + name + "%'");
				sb2.append(" and ");
				request.setAttribute("name", name);

			}

			sb.append(" kemulock=0  order by id desc ");
			String where = sb.toString();
			sb2.append(" kemulock=0  ");
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = kemuDao.selectBeanCount(where2);
			request.setAttribute("list", kemuDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			request.setAttribute("url", "method!kemulist2");
			this.setUrl("kemu/kemulist2.jsp");
			return SUCCESS;
		}
	
		//报考该科目操作
		public void kemudelete2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			String riqi = Util.getTime2();
			Kemu kemu = kemuDao.selectBean(" where id= "
					+ request.getParameter("id") +" and kaishi<= '"+riqi+"' and jieshu>='"+riqi+"' " );
			if(kemu==null){
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(Util.tiaozhuan2("操作失败，该科目不在报考日期以内", "method!kemulist2", "kemulist2"));
				out.flush();
				out.close();
				return;
			}
			
			HttpSession session = request.getSession();
			User user = (User)session.getAttribute("user");
			
			Baokao bean = baokaoDao.selectBean(" where kemu.id= "+kemu.getId()+" and user.id= "+user.getId());
			if(bean==null){
				bean = new Baokao();
				bean.setCreatetime(new Date());
				bean.setKemu(kemu);
				bean.setUser(user);
				bean.setZhuangtai("未审核");
				baokaoDao.insertBean(bean);
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(Util.tiaozhuan2("操作成功", "method!kemulist2", "kemulist2"));
				out.flush();
				out.close();
			}else{
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(Util.tiaozhuan2("操作失败，你已经报考过该科目", "method!kemulist2", "kemulist2"));
				out.flush();
				out.close();
			}

		}
		
		
		//审核报考
		public String baokaolist() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");

			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");

			if (name != null && !"".equals(name)) {
				sb.append(" kemu.name like '%" + name + "%'");
				sb.append(" and ");
				sb2.append(" kemu.name like '%" + name + "%'");
				sb2.append(" and ");
				request.setAttribute("name", name);

			}

			sb.append(" 1=1  order by id desc ");
			String where = sb.toString();
			sb2.append(" 1=1  ");
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = baokaoDao.selectBeanCount(where2);
			request.setAttribute("list", baokaoDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			request.setAttribute("url", "method!baokaolist");
			this.setUrl("baokao/baokaolist.jsp");
			return SUCCESS;
		}
		
		
		//审核通过操作
		public void baokaodelete() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			Baokao bean = baokaoDao.selectBean(" where id= "+ request.getParameter("id")  );
			if("未审核".equals(bean.getZhuangtai())){
				bean.setZhuangtai("审核通过");
			}
			
			baokaoDao.updateBean(bean);
			Kemu kemu = kemuDao.selectBean(" where id= "+bean.getKemu().getId());
			kemu.setRenshu(kemu.getRenshu()+1);
			kemuDao.updateBean(kemu);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(Util.tiaozhuan2("操作成功", "method!baokaolist", "baokaolist"));
			out.flush();
			out.close();
			
			

		}
		
		
		//审核不通过操作
		public void baokaodelete2() throws IOException {
			HttpServletRequest request = ServletActionContext.getRequest();
			Baokao bean = baokaoDao.selectBean(" where id= "+ request.getParameter("id")  );
			if("未审核".equals(bean.getZhuangtai())){
				bean.setZhuangtai("审核不通过");
			}
			
			baokaoDao.updateBean(bean);
	
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(Util.tiaozhuan2("操作成功", "method!baokaolist", "baokaolist"));
			out.flush();
			out.close();
		}
		
		
		//打印准考证
		public String baokaolist2() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String name = request.getParameter("name");

			StringBuffer sb = new StringBuffer();
			sb.append(" where ");
			StringBuffer sb2 = new StringBuffer();
			sb2.append(" where ");

			if (name != null && !"".equals(name)) {
				sb.append(" kemu.name like '%" + name + "%'");
				sb.append(" and ");
				sb2.append(" kemu.name like '%" + name + "%'");
				sb2.append(" and ");
				request.setAttribute("name", name);

			}

			HttpSession session = request.getSession();
			User user = (User)session.getAttribute("user");
			
			sb.append(" user.id="+user.getId()+"  order by id desc ");
			String where = sb.toString();
			sb2.append(" user.id="+user.getId());
			String where2 = sb2.toString();

			int currentpage = 1;
			int pagesize = 20;
			if (request.getParameter("pageNum") != null) {
				currentpage = Integer.parseInt(request.getParameter("pageNum"));
				pagesize = Integer.parseInt(request.getParameter("numPerPage"));
			}
			int total = baokaoDao.selectBeanCount(where2);
			request.setAttribute("list", baokaoDao.selectBeanList((currentpage - 1)
					* pagesize, pagesize, where));
			request.setAttribute("totalCount", total);
			request.setAttribute("ps", pagesize);
			request.setAttribute("pn", currentpage);
			request.setAttribute("url", "method!baokaolist2");
			this.setUrl("baokao/baokaolist2.jsp");
			return SUCCESS;
		}
		
		
		//打印准考证
		public String baokaoupdate() {
			HttpServletRequest request = ServletActionContext.getRequest();
			Baokao bean = baokaoDao.selectBean(" where id= "
					+ request.getParameter("id"));
			request.setAttribute("bean", bean);
			
			this.setUrl("baokao/baokaoupdate.jsp");
			return SUCCESS;
		}
		
}
